﻿<?
import ("stock_price_PriceVo");
import ("stock_price_LivePriceVo");
class stock_analyser_ChartView extends jsml_View{
	public $arrayInfo;
	public $key;
	public function construct($key) {
		parent::construct(null);
		if ($this->config->ChartView == $key) {
			$this->w = 1;
			$this->width = round((3.2*$this->screenW)/2) - 50;
			$this->height = $this->screenH+47;
		} else if ($this->config->ChartView2 == $key) {
			$this->w = 5;
			//////$this->width = $this->screenW + 480;
			$this->width = round((3.2*$this->screenW)/2) - 50;
			$this->height = $this->screenH+47;
		} else if ($this->config->ChartView3 == $key) {
			$this->key = $key;
			$this->w = 5;
			$this->width = $this->screenW + 70;
			$this->height = $this->screenH+47;
		}
$this->arrayInfo = array(
	 "AGC" => "OK 04/06/2011<br/>
		+ AGC: 10/6, ngày GDKHQ chốt DS tổ chức ĐHCĐ thường niên năm 2011 (03/06/2011 16:00)
	"
	,"VCG" => "OK 04/06/2011<br/>
		- VCG: Lỗ ròng 18,98 tỷ đồng quý I/2011 (22/05/2011 08:17)
	"
);
		return $this;
	}
	
	public static $startDate2  = "2011-08-16";
	
	public static $startDate = "2011-09-13";
	
	public static $startDate3 = "2011-07-29";
	
	public static $startDate4 = "2011-06-14";

	public $w=5;
	public $padX = 10;
	public $valueShift = 0;
	public $ox=40;
	public $oy=40;

	public $stickY = 11;

	public $padY = 122;
	public $ma1 = 4;
	public $ma2 = 6;
	public $ma3 = 8;
	public $ma4 = 10;

	public $minStockDataX;
	public $maxStockDataX;
	public $rangeX;
	public $scaleX;

	public $minStockDataY = 30;
	public $maxStockDataY = 80;
	public $rangeY;
	public $scaleY;

	public $minStockDataY2 = 0;
	public $maxStockDataY2 = 3000000000;
	public $rangeY2;
	public $scaleY2;

	public $minStockDataY3 = 0;
	public $maxStockDataY3 = 100;
	public $rangeY3;
	public $scaleY3;

	public $priceDtArray = array();
	public $canvas;
	public $context;
	public $divCanvasText;
	
	public $width;
	public $height;

	public $tableView;
	protected function configCss($key) {
		$css = parent::configCss($key);
		if ($this->config->ChartView == $key) {
			$_::extend($css, array(
				"background-color" => "white",
				"height" => $this->height,
				"width" => $this->width
			));
		}
		return $css;
	}
	public function view(){
		$this->canvas = $_("<canvas></canvas>")->appendTo($this->div)->css($this->getCss($this->config->ChartView, array("border"=>"solid 1px")));
		$this->canvas->attr(array("width"=>$this->width, "height"=>$this->height));
		$this->divCanvasText = $_("<div></div>")->appendTo($this->div)->css($this->getCss(null));

		$canvas = $this->canvas->get(0);
		/*<@ if (typeof G_vmlCanvasManager != 'undefined') G_vmlCanvasManager->initElement($canvas); @>*/
		$this->context = $canvas->getContext('2d');

		return $this;
	}
	public function viewPaint(){
		$this->context->clearRect(0, 0, $this->width, $this->height);
		$this->divCanvasText->html("");
		$this->makeMa();
		$this->saveDtParams();
		$this->paintOxy();
		$this->paint();
	}

	public function getX($i){
		return ($this->width - $this->ox - $this->padX) - ($this->w+1)*$i;
	}
	public function getI($x){
		return round( (($this->width - $this->ox - $this->padX) - $x)/($this->w+1) );
	}
	public function saveScale(){
		//$this->rangeX = $this->maxStockDataX - $this->minStockDataX;
		//$this->scaleX = ($this->width - $this->ox - $this->padX) / $this->rangeX;

		$this->rangeY = $this->maxStockDataY - $this->minStockDataY;
		$this->scaleY = ($this->height - $this->oy - $this->padY) / $this->rangeY;

		$this->rangeY2 = $this->maxStockDataY2 - $this->minStockDataY2;
		$this->scaleY2 = ($this->height - $this->oy) / $this->rangeY2;

		$this->rangeY3 = $this->maxStockDataY3 - $this->minStockDataY3;
		$this->scaleY3 = ($this->height - $this->oy) / $this->rangeY3;
	}
	public function getY($y){
		return ($this->height - $this->oy - $this->padY) - ($y- $this->minStockDataY) * $this->scaleY;
	}
	public function getY2($y){
		return ($this->height - $this->oy - $this->padY) - ($y- $this->minStockDataY2) * $this->scaleY2;
	}
	public function getY3($y){
		return ($this->height - $this->oy - $this->padY) - ($y- $this->minStockDataY3) * $this->scaleY3;
	}

	public $minMaxPadFactor = 1.93;
	public function saveDtParams(){
		$vo = new stock_price_LivePriceVo();
		$minStockDataY = 9999999;
		$iMin=0;
		$DAY_BREAK = 1*60;
		for($i=1; $i<$this->priceDtArray->length; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			if($stockData->close == 0 || $stockData->close==null) continue;
			if($stockData->close < $minStockDataY){
				$minStockDataY = $stockData->close;
				$iMin = $i;
			}
			if($x<$this->padX) break;
			//if($this->w==5 && $i>=60) break;
			if($this->w==5 && $i>=$DAY_BREAK) break;
			//if($this->w==1 && $i>=120) break;
		}
			
		$maxStockDataY = 0;
		for($i=1; $i<$this->priceDtArray->length; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			if($stockData->close == 0 || $stockData->close==null) continue;
			if($stockData->close > $maxStockDataY) {
				$maxStockDataY = $stockData->close;
			}
			if($x<$this->padX) break;
			//if($this->w==5 && $i>=60 && $i>=$iMin) break;
			if(($this->w==5 && $i>=$DAY_BREAK) || $i>=$iMin) break;
			///if($this->w==5 && $i>=$DAY_BREAK) break;
			//if($this->w==1 && $i>=120) break;
		}
		
		if($this->priceDtArray[0] != null){
			if($this->priceDtArray[0]["stock_symbol"] == "VNINDEX") $this->minMaxPadFactor = 1.5;
			if($this->priceDtArray[0]["stock_symbol"] == "HAINDEX") $this->minMaxPadFactor = 1.5;
		}
		if($this->config->ChartView3 == $this->key) $this->minMaxPadFactor = $this->minMaxPadFactor/2;

		if($minStockDataY==null || $minStockDataY==0) $minStockDataY = 10;
		//alert($minStockDataY);
		
		$this->minStockDataY = 0.9 * $minStockDataY;
		$this->maxStockDataY = 1.1 * $this->minMaxPadFactor * $minStockDataY;
		
		if($maxStockDataY > $this->maxStockDataY){
			$this->maxStockDataY = 1.1*$maxStockDataY;
			$this->minStockDataY = 1/(1.1 * $this->minMaxPadFactor) * $this->maxStockDataY;
		}
		
		//////$this->minStockDataY = 1/2 * $this->priceDtArray[0]->close;
		//////$this->maxStockDataY = 2 * $this->priceDtArray[0]->close;
			
		////if($maxStockDataY > $this->maxStockDataY) $this->maxStockDataY = 1.05*$maxStockDataY;

		$maxStockDataY2 = 0;
		$avg = 0;
		$count = 0;
		$div = 0;
		for($i=0; $i<$this->priceDtArray->length; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			$value = $stockData["volume"];//$vo->getValue($stockData);
			if($value > $maxStockDataY2) $maxStockDataY2 = $value;
			$avg += $value;
			$count++;
			if($x<$this->padX) break;
			//if($this->w==5 && $i>=60) break;
			if($this->w==5 && $i>=$DAY_BREAK) break;
		}
		/*
		$avg = $avg/$count;
		$count = 0;
		for($i=1; $i<$this->priceDtArray->length; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			$div += Math::pow($vo->getLiveValue($stockData) - $avg, 2);
			$count++;
			if($x<$this->padX) break;
			if($i>=$this->ma2) break;
		}
		$div = Math::pow($div/$count, 1/2);
		*/
		$this->maxStockDataY2 = 6*$maxStockDataY2;
		if($this->key == $this->config->ChartView3 /*&& $this->priceDtArray[0]["stock_symbol"] != "VNINDEX" && $this->priceDtArray[0]["stock_symbol"] != "HAINDEX"*/ ){
			$this->maxStockDataY2 = 1*10;
		}
			
		$this->saveScale();
	}

	public function paintText($text, $left, $top, $color){
		if($color==null) $color='black';
		return $_(implode("", array("<div>",$text,"</div>")))->css(array("position"=>"absolute", "left"=>$left, "top"=>$top, "color"=>$color))->appendTo($this->divCanvasText);
	}

	public function paint(){
		$this->makeFirst();
		$this->paintValue();
		if($this->w==5) {
			$this->paintMa();
			$this->paintCandleStick();
		}
		else{
			$this->paintClose();
			//$this->paintMa();
		}
		$this->paintTable();
	}

	public function paintCandleStick(){
		$this->context->restore();
		$this->context->globalAlpha=2;
		for($i=0; $i<$this->priceDtArray->length-1; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			if($x<$this->padX) break;

			if($stockData->close > $stockData->open){
				$this->context->fillStyle = 'blue';
			}
			else{
				$this->context->fillStyle = 'hotpink';
			}
			
			if($this->priceDtArray[$i+1] != null && $this->priceDtArray[$i+1]->close !=0) {
				$percent = 100*($stockData->close - $this->priceDtArray[$i+1]->close)/ $this->priceDtArray[$i+1]->close;
				if($percent >= 3) $this->context->fillStyle = 'Black';
				if($percent <= -3) $this->context->fillStyle = 'Red';
			}

			$deltaClose = $this->getY($stockData->close) - $this->getY($stockData->open);
			$deltaHigh = $this->getY($stockData->high) - $this->getY($stockData->low);
			if($deltaClose==0) $deltaClose = 1;
			if($deltaHigh==0) $deltaHigh = 1;
			$this->context->fillRect($x - ($this->w+1)/2, $this->getY($stockData->open), $this->w, $deltaClose );
			$this->context->fillRect($x-1, $this->getY($stockData->low), 1, $deltaHigh );
			if($i==0 && $this->w==5){
				$x = $this->getX(-1);
					
				$this->context->fillStyle = 'blue';
				$PERCENT = 1.05;
				if($stockData->symbol == "VNINDEX") $PERCENT = 1.01;
				else if($stockData->symbol == "HAINDEX") $PERCENT = 1.02;
				$deltaClose = $this->getY($PERCENT * $stockData->close) - $this->getY($stockData->close);
				$deltaHigh = $this->getY($PERCENT * $stockData->close) - $this->getY($stockData->close);
				$this->context->fillRect($x - ($this->w+1)/2, $this->getY($stockData->close), $this->w, $deltaClose );
				$this->context->fillRect($x-1, $this->getY($stockData->close), 1, $deltaHigh );
					
				$this->context->fillStyle = 'red';
				$PERCENT = 1 - ($PERCENT - 1);
				$deltaClose = $this->getY($PERCENT * $stockData->close) - $this->getY($stockData->close);
				$deltaHigh = $this->getY($PERCENT * $stockData->close) - $this->getY($stockData->close);
				$this->context->fillRect($x - ($this->w+1)/2, $this->getY($stockData->close), $this->w, $deltaClose );
				$this->context->fillRect($x-1, $this->getY($stockData->close), 1, $deltaHigh );
			}
		}
		/*
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Black';
		$this->context->lineWidth=1;
		$this->context->globalAlpha=0.5;
		$x0 = $this->getX(0);
		$y0 = $this->getY2(0);
		for($i=0; $i<$this->priceDtArray->length; $i = $i+20){
			$x = $this->getX($i);
			if($x<$this->padX) break;

			$this->context->moveTo($x, $y0);
			$this->context->lineTo($x0, $y0 - 1*($this->w+1)*$i);
		}
		$this->context->stroke();
		*/
		$this->context->globalAlpha=1;
	}
	
	public function paintClose(){
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Black';
		$this->context->lineWidth=1;
		$this->context->globalAlpha=0.6;
		for($i=0; $i<$this->priceDtArray->length; $i++){
			$x = $this->getX($i);
			if($x<$this->padX) break;
			$value = $this->priceDtArray[$i]->close;
			if($value==null) continue;
			$y = $this->getY($value);
			if($i==0){
				$this->context->moveTo($x, $y);
			} else{
				$this->context->lineTo($x, $y);
			}
		}

		$this->context->stroke();
		$this->context->globalAlpha=1;
	}
	public function paintValue(){
		$this->context->restore();
		$this->context->globalAlpha=1;
		$vo = new stock_price_LivePriceVo();
		if($this->w==5) for($i=0; $i<$this->priceDtArray->length; $i++){
			$stockData = $this->priceDtArray[$i];
			$x = $this->getX($i);
			if($this->key == $this->config->ChartView3 /*&& $this->priceDtArray[0]["stock_symbol"] != "VNINDEX" && $this->priceDtArray[0]["stock_symbol"] != "HAINDEX"*/ ){
				if($this->priceDtArray[0]["stock_symbol"] == "VNINDEX"){
					$chartView = $this->config->getBean($this->config->AnalyserView)->chartViewMap["vn"];
				} else if($this->priceDtArray[0]["stock_symbol"] == "HAINDEX"){
					$chartView = $this->config->getBean($this->config->AnalyserView)->chartViewMap["ha"];
				} else {
					$chartView = $this->config->getBean($this->config->AnalyserView)->chartViewMap["stock"];
				}
				$y2 = null;
				if($chartView->priceDtArray[0]->volume1!=0){
					$y2 = $vo->getLiveVolumeSub($stockData);
					$y2 = $y2/$chartView->priceDtArray[0]["volume1"];
				}
			} else {
				$y2 = $vo->getLiveVolume($stockData);
			}
			if($y2==null) continue;
			if($x<$this->padX) break;
			if( $stockData->close >= $stockData->open) $this->context->fillStyle = 'Green';
			else $this->context->fillStyle = 'HotPink';
			$this->context->fillRect($x- ($this->w+1)/2, $this->getY2(0), $this->w , -$y2*$this->scaleY2);

			if($i==0){
				//$buyValue = $stockData->buyPrice3*$stockData->buyVolume3 + $stockData->buyPrice2*$stockData->buyVolume2 + $stockData->buyPrice1*$stockData->buyVolume1;
				//$sellValue = $stockData->sellPrice3*$stockData->sellVolume3 + $stockData->sellPrice2*$stockData->sellVolume2 + $stockData->sellPrice1*$stockData->sellVolume1;
				//$buyValue = 0;
				//$this->context->fillStyle = 'DodgerBlue';
				//$this->context->fillRect($x- ($this->w+1)/2, $this->getY2($y2), $this->w , -$buyValue*$this->scaleY2);
			}
		}
		$this->context->globalAlpha=1;

		$this->context->beginPath();
		$this->context->strokeStyle = 'Black';
		$this->context->lineWidth=2;
		if($this->key == $this->config->ChartView3 /*&& $this->priceDtArray[0]["stock_symbol"] != "VNINDEX" && $this->priceDtArray[0]["stock_symbol"] != "HAINDEX"*/ ){
			for($i=0; $i<$this->priceDtArray->length; $i++){
				$x = $this->getX($i);
				if($x<$this->padX) break;
				$value = 1;
				if($value==null) continue;
				$y = $this->getY2($value);
				if($i==0){
					$this->context->moveTo($x, $y);
				} else{
					$this->context->lineTo($x, $y);
				}
			}
		} else {
			for($i=0; $i<$this->priceDtArray->length; $i++){
				$x = $this->getX($i);
				if($x<$this->padX) break;
				$value = $this->priceDtArray[$i]->maVolume;
				if($value==null) continue;
				$y = $this->getY2($value);
				if($i==0){
					$this->context->moveTo($x, $y);
				} else{
					$this->context->lineTo($x, $y);
				}
			}
		}
		$this->context->stroke();
	}
	public function paintMa(){
		$this->context->restore();
		$vo = new stock_price_LivePriceVo();
		for($k=4; $k>=1; $k--){
			//if($k==1) continue;
			$this->context->beginPath();
			$this->context->lineWidth=2;
			if($k==1){
				$this->context->strokeStyle='Teal';
			}
			else if($k==2){
				$this->context->strokeStyle='black';
			}
			else if($k==3){
				$this->context->strokeStyle='Sienna';
			}
			else if($k==4){
				$this->context->strokeStyle='Purple';
			}
			for($i=0; $i<$this->priceDtArray->length; $i++){
				$x = $this->getX($i);
				if($x<$this->padX) break;
				$stockData = $this->priceDtArray[$i];
				if($k==1) $y = $stockData->ma1;
				else if($k==2) $y = $stockData->ma2;
				else if($k==3) $y = $stockData->ma3;
				else if($k==4) $y = $stockData->ma4;
				if($y==null) break;
				$y = $this->getY($y);
				if($i==0){
					$this->context->moveTo($x, $y);
				} else{
					$this->context->lineTo($x, $y);
				}
			}
			$this->context->stroke();
		}
		/*
		$y0 = $this->getY($this->minStockDataY);
		$y = $this->getY($this->priceDtArray[0]->ma1);
		$x0 = $this->getX(0);
		for($i=15; $i<90; $i = $i + 15){
			$this->context->restore();
			$this->context->beginPath();
			$this->context->strokeStyle = 'Black';
			$this->context->lineWidth=1;
			$this->context->globalAlpha=1;
		
			if($i == 45) {
				$this->context->lineWidth=2;
				$this->context->strokeStyle = 'Red';
			}
			else {
				$this->context->lineWidth=1;
				$this->context->strokeStyle = 'Black';
			}
			$x = $x0 - 1/tan(deg2rad($i)) * ($this->priceDtArray[0]->ma1 - $this->minStockDataY) * $this->scaleY;

			$this->context->moveTo($x0, $y);
			$this->context->lineTo($x, $y0);
			$this->context->stroke();
		}
		*/
		$this->context->globalAlpha=1;
	}
	
	public function paintTable(){
		$livePriceView = new stock_price_LivePriceView();
		$this->context->restore();
		$stock_symbol = $this->priceDtArray[0]["stock_symbol"];
		$livePriceView = new stock_price_LivePriceView();
		
		//$this->paintText("<b><span style='color:black'>".$stock_symbol.".".$this->priceDtArray[0]["market"]."</span></b>", $this->width - 250, 60);
		$this->paintText("<b>".$livePriceView->formatValue($this->priceDtArray[0]["value"])."_<span style='color:red'>".$livePriceView->formatValue($this->priceDtArray[0]["value2"])."</span>_".$livePriceView->formatPrice($this->priceDtArray[0]["value2Percent"])."</b>", $this->width - 160, 60);
		$this->paintText("<b><span style='color:red'>".$livePriceView->formatPrice($this->priceDtArray[0]["megaCount"])."</span>_<span style='color:red'>".$livePriceView->formatPrice($this->priceDtArray[0]["greatAll"])."</span>_<span style='color:red'>".$livePriceView->formatPrice($this->priceDtArray[0]["minClose5"])."</span></b>", $this->width - 160, 80);
		$this->paintText("<span style='color:black'><b>".$stock_symbol.".".$this->priceDtArray[0]["market"].": </b>".$this->arrayInfo[$stock_symbol]."</span>", $this->width - 570, 60)->css("width", 400);

		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Black';
		$this->context->lineWidth=1;
		
		$x1 = $this->getX(0);
		$x2 = $this->getX(120);
		
		$this->context->restore();
		$this->context->beginPath();
		$y = $this->getY($this->priceDtArray[0]["min"]);
		$this->context->moveTo($x1, $y);
		$this->context->lineTo($x2, $y);
		$this->context->stroke();
		/*
		$y = $this->getY(1.10*$this->priceDtArray[0]["min"]);
		$this->context->moveTo($x1, $y);
		$this->context->lineTo($x2, $y);
		*/
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Blue';
		$this->context->lineWidth=2;
		$y = $this->getY(1.20*$this->priceDtArray[0]["min"]);
		$this->context->moveTo($x1, $y);
		$this->context->lineTo($x2, $y);
		$this->context->stroke();
		
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Black';
		$this->context->lineWidth=1;
		$y = $this->getY(1.50*$this->priceDtArray[0]["min"]);
		$this->context->moveTo($x1, $y);
		$this->context->lineTo($x2, $y);
		$this->context->stroke();
		
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'Blue';
		$this->context->lineWidth=2;
		
		$y = $this->getY(2.00*$this->priceDtArray[0]["min"]);
		$this->context->moveTo($x1, $y);
		$this->context->lineTo($x2, $y);
		$this->context->stroke();
		
		return;
		
		if($this->tableView==null) return;
		$livePriceView = new stock_price_LivePriceView();
		$this->context->restore();
		$stock_symbol = $this->priceDtArray[1]["stock_symbol"];
		if($this->tableView->tableDtMap[$stock_symbol]!=null){
			$this->paintText("<b><span style='color:black'>".$stock_symbol.".".$this->tableView->tableDtMap[$stock_symbol]["market"]."</span></b>", $this->width - 220, 60);
			$this->paintText("<b>".$this->tableView->formatValue($this->tableView->tableDtMap[$stock_symbol]["value"])."_<span style='color:red'>".$this->tableView->formatValue($this->tableView->tableDtMap[$stock_symbol]["value2"])."</span>_".$livePriceView->formatPrice($this->tableView->tableDtMap[$stock_symbol]["valuePercent"])."</b>", $this->width - 160, 60);
			$this->paintText("<b><span style='color:red'>".$this->tableView->tableDtMap[$stock_symbol]["megaCount"]."</span>_<span style='color:red'>".$livePriceView->formatPrice($this->tableView->tableDtMap[$stock_symbol]["minMax"])."</span>_<span style='color:red'>".$livePriceView->formatPrice($this->tableView->tableDtMap[$stock_symbol]["minClose3"])."</span></b>", $this->width - 160, 80);
		}
	}

	public function paintOxy(){
		$this->context->restore();
		$this->context->beginPath();
		$this->context->strokeStyle = 'black';
		$this->context->globalAlpha = 1;
		$this->context->lineWidth = 1;
		//Draw Oy
		$ox = 0;
		$ox2 = $this->width - $this->ox;
		$this->context->moveTo($ox, $this->getY($this->minStockDataY) + $this->padY);
		$this->context->lineTo($ox, $this->getY($this->maxStockDataY));
		$this->context->moveTo($ox2, $this->getY($this->minStockDataY) + $this->padY);
		$this->context->lineTo($ox2, $this->getY($this->maxStockDataY));

		$one = ($this->maxStockDataY - $this->minStockDataY)/($this->stickY-1);
		for($value=$this->minStockDataY; $value<=$this->maxStockDataY; $value = $value+$one){
			$y = $this->getY($value);
			if($y-8<=0 || $y>=$this->getY($this->minStockDataY)) continue;
			/*$this->context.moveTo(ox, y);
			 $this->context.lineTo(ox-8, y);
			 $this->paintText(Math.jsmlRound(value,0), ox - $this->ox, y - 8);
			 */
			$this->context->moveTo($ox2, $y);
			$this->context->lineTo($ox2+4, $y);
			$this->paintText(round($value,1), $ox2 + $this->ox - 34, $y - 8);
		}

		//draw Ox
		$y = $this->getY($this->minStockDataY);// + $this->padY;
		$this->context->moveTo($ox, $y);
		$this->context->lineTo($ox2, $y);

		for($i=0; $i<$this->priceDtArray->length; $i++){
			$x = $this->getX($i);
			if($x<$this->padX) break;
			if($i%$this->ma3==0){
				$this->context->moveTo($x, $y);
				$this->context->lineTo($x, $y + 8 );
				$stockDate = $this->priceDtArray[$i]->stockDate;
				if($this->w==1){
					$stockDate = substr($stockDate, 0, 7);
				}
				if($x - 40>=0) $this->paintText($stockDate, $x - 40, $y + 8);
			}
			/*if(i==$this->ma2 || i==$this->ma1){
				$this->context.moveTo(x, y);
				$this->context.lineTo(x, $this->getY($this->maxStockDataY) );
				}*/
		}

		$this->context->closePath();
		$this->context->stroke();


		for($i=0; $i<$this->priceDtArray->length; $i++){
			$x = $this->getX($i);
			if($x<$this->padX) break;
			if(((substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate || substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate2 || substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate3 || substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate4) )){
			//if($i==10 || $i==20 || $i==60 || $i==120){
				$this->context->beginPath();
				if($i==120) $this->context->strokeStyle='Green';
				if($i==10) $this->context->strokeStyle='Purple';
				if($i==20) $this->context->strokeStyle='Teal';
				if($i==60) $this->context->strokeStyle='Sienna';
				if(substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate ) $this->context->strokeStyle='Purple';
				if(substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate2 ) $this->context->strokeStyle='Sienna';
				if(substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate3 ) $this->context->strokeStyle='Teal';
				if(substr($this->priceDtArray[$i]->stockDate,0,10) == stock_analyser_ChartView::$startDate4 ) $this->context->strokeStyle='Green';
				$this->context->globalAlpha=1;
				$this->context->lineWidth=1;
				$this->context->moveTo($x, $y);
				$this->context->lineTo($x, $this->getY($this->maxStockDataY) );
				$this->context->closePath();
				$this->context->stroke();
			}
		}
		return;
	}
	
	public function makeMa($limit){
		$vo = new stock_price_LivePriceVo();
		$length = count($this->priceDtArray);
		for($i=0; $i<$length; $i++){
			if($limit != null && $i >= $limit) break;
			if($this->priceDtArray[$i]["maValue"] != null) continue;
			if($this->priceDtArray[$i]["maVolume"] != null) continue;
			$maValue = 0;
			$maVolume = 0;
			$maCount = 0;
			for($j=0; $j<$this->ma4; $j++){
				if($i+$j >= $length) break;
				$stockData = $this->priceDtArray[$i+$j];
				$maValue += $vo->getValue($stockData);
				$maVolume += $stockData["volume"];
				$maCount ++;
			}
			$this->priceDtArray[$i]["maValue"] = $maValue/$maCount;
			$this->priceDtArray[$i]["maVolume"] = $maVolume/$maCount;
		}

		for($i=0; $i<$length; $i++){
			if($limit != null && $i >= $limit) break;
			if($this->priceDtArray[$i]["ma1"] != null
			&& $this->priceDtArray[$i]["ma2"] != null
			&& $this->priceDtArray[$i]["ma3"] != null
			&& $this->priceDtArray[$i]["ma4"] != null) continue;
			$value = 0;
			for($j=0; $j<$this->ma4; $j++){
				if($i+$j >= $length) break;
				$stockData = $this->priceDtArray[$i+$j];
				$value += $stockData["close"];
				//if(i==0&&j==0) value += stockData.buyPrice3*stockData.buyVolume3 + stockData.buyPrice2*stockData.buyVolume2 + stockData.buyPrice1*stockData.buyVolume1;
				if($j==$this->ma1-1) {
					$this->priceDtArray[$i]["ma1"] = $value/$this->ma1;
					if($this->priceDtArray[$i]["ma1"] !=0) $this->priceDtArray[$i]["maPercent1"] = 100*$this->priceDtArray[$i]["high"]/$this->priceDtArray[$i]["ma1"];
					if($this->priceDtArray[$i]["ma1"] !=0) $this->priceDtArray[$i]["maPer1"] = 100*$this->priceDtArray[$i]["close"]/$this->priceDtArray[$i]["ma1"] - 100;
				} else if($j==$this->ma2-1) {
					$this->priceDtArray[$i]["ma2"] = $value/$this->ma2;
				} else if($j==$this->ma3-1) {
					$this->priceDtArray[$i]["ma3"] = $value/$this->ma3;
					if($this->priceDtArray[$i]["ma3"] !=0) $this->priceDtArray[$i]["maPercent3"] = 100*$this->priceDtArray[$i]["high"]/$this->priceDtArray[$i]["ma3"];
				} else if($j==$this->ma4-1) {
					$this->priceDtArray[$i]["ma4"] = $value/$this->ma4;
					if($this->priceDtArray[$i]["ma4"] !=0) $this->priceDtArray[$i]["maPercent4"] = 100*$this->priceDtArray[$i]["high"]/$this->priceDtArray[$i]["ma4"];
					
					if($this->priceDtArray[$i]["ma4"] !=0) $this->priceDtArray[$i]["maPercent2"] = 100*$this->priceDtArray[$i]["high"]/$this->priceDtArray[$i]["ma4"];
					if($this->priceDtArray[$i]["ma4"] !=0) $this->priceDtArray[$i]["maPercent12"] = 100*$this->priceDtArray[$i]["ma1"]/$this->priceDtArray[$i]["ma4"];
					if($this->priceDtArray[$i]["ma4"] !=0) $this->priceDtArray[$i]["maPer12"] = 100*$this->priceDtArray[$i]["ma1"]/$this->priceDtArray[$i]["ma4"] - 100;
					
					$maCode = array();
					if($this->priceDtArray[$i]["maPercent1"] > 100) array_push($maCode, "P1");
					else array_push($maCode, "M1");
					if($this->priceDtArray[$i]["maPercent2"] > 100) array_push($maCode, "P1");
					else array_push($maCode, "M1");
					if($this->priceDtArray[$i]["maPercent3"] > 100) array_push($maCode, "P1");
					else array_push($maCode, "M1");
					if($this->priceDtArray[$i]["maPercent4"] > 100) array_push($maCode, "P1");
					else array_push($maCode, "M1");
					$this->priceDtArray[$i]["maCode"] = implode("", $maCode);
				}
			}
		}
	}
	
	public function makePrice($limit){
		$vo = new stock_price_LivePriceVo();
		$length = count($this->priceDtArray);
		for($i=0; $i<$length - 1; $i++){
			if($limit != null && $i >= $limit) break;
			if($this->priceDtArray[$i+1]["close"]!=0) $this->priceDtArray[$i]["closePercent"] = 100*($this->priceDtArray[$i]["close"] - $this->priceDtArray[$i+1]["close"])/$this->priceDtArray[$i+1]["close"];
		}
	}
	
	public function makeFirst(){
		$length = count($this->priceDtArray);
		$value2 = 0;
		$value2Count = 0;
		$count1 = 0;
		$count2 = 0;
		$count3 = 0;
		$count4 = 0;
		$megaCount1 = 0;
		$megaCount2 = 0;
		$megaCount3 = 0;
		$megaCount4 = 0;
		$PERCENT = 4;
		$volume1 = 0;
		$volumeCount = 0;
		for($i=0; $i < $length; $i++){
			if($this->priceDtArray[$i+1]==null || $this->priceDtArray[$i+1]["close"]==0 || $this->priceDtArray[$i]["low"]==0) continue;
			$delta = 100*($this->priceDtArray[$i]["close"] - $this->priceDtArray[$i+1]["close"]) / $this->priceDtArray[$i+1]["close"];
			$deltaAbs = abs($delta);
			if($deltaAbs >= 10){
				//$this->priceDtArray[0]["isDevided"] = 1;
				continue;
			}
			
			$megaCount4 += $deltaAbs;
			$count4++;
			if($i<$this->ma4){
				$volume1 += $this->priceDtArray[$i]["volume"];
				$volumeCount ++;
			}
			if($i<$this->ma3){
				$megaCount3 += $deltaAbs;
				$count3++;
			}
			if($i<20){
				$megaCount2 += $deltaAbs;
				$count2++;
				$value2 += $this->priceDtArray[$i]["close"]*$this->priceDtArray[$i]["volume"];
				$value2Count ++;
			}
			if($i<$this->ma1){
				$megaCount1 += $deltaAbs;
				$count1++;
			}
		}
		
		if($count4!=0) $megaCount4 = ($megaCount4)/$count4;
		if($count3!=0) $megaCount3 = ($megaCount3)/$count3;
		if($count2!=0) $megaCount2 = ($megaCount2)/$count2;
		if($count1!=0) $megaCount1 = ($megaCount1)/$count1;
		
		if($volumeCount!=0) $volume1 = $volume1/$volumeCount;
		
		$megaCount = $megaCount4;
		//if($megaCount3 > $megaCount) $megaCount = $megaCount3;
		//if($megaCount2 > $megaCount) $megaCount = $megaCount2;
		//if($megaCount1 > $megaCount) $megaCount = $megaCount1;
		$this->priceDtArray[0]["megaCount"] = $megaCount;
		$this->priceDtArray[0]["megaCount4"] = $megaCount4;
		$this->priceDtArray[0]["megaCount3"] = $megaCount3;
		$this->priceDtArray[0]["megaCount2"] = $megaCount2;
		$this->priceDtArray[0]["megaCount1"] = $megaCount1;
		$this->priceDtArray[0]["volume1"] = $volume1;
		
		if($value2Count!=0) $value2 = $value2/$value2Count;
		$this->priceDtArray[0]["value2"] = $value2;
		
		$min = 999999;
		$imin = 0;
		$max = 0;
		$imax = 0;
		$length = count($this->priceDtArray);
		$haveBreak = false;
		for($i=0; $i < $length; $i++){
			if($this->priceDtArray[$i]["close"] < $min){
				$min = $this->priceDtArray[$i]["close"];
				$imin = $i;
			}
			
			if($this->priceDtArray[$i]["close"] > $max){
				$max = $this->priceDtArray[$i]["close"];
				$imax = $i;
			}
			
			if( substr($this->priceDtArray[$i]["stockDate"],0,10) == stock_analyser_ChartView::$startDate) {
				$haveBreak = true;
				break;
			}
		}
		/*
		for($i=0; $i <= $imin; $i++){
			if($this->priceDtArray[$i]["close"] > $max){
				$max = $this->priceDtArray[$i]["close"];
				$imax = $i;
			}
		}
		*/
		$this->priceDtArray[0]["imin"] = $imin;
		$this->priceDtArray[0]["min"] = $min;
		$this->priceDtArray[0]["imax"] = $imax;
		$this->priceDtArray[0]["max"] = $max;
		
		$this->priceDtArray[0]["minMax"] = 0;
		if($min!=0 && $max!=0) $this->priceDtArray[0]["minMax"] = 100*($max - $min)/$min;
		//if($imin < $imax) $this->priceDtArray[0]["minMax"] = -$this->priceDtArray[0]["minMax"];
		if(!$haveBreak) $this->priceDtArray[0]["minMax"] = 0;
		
		$this->priceDtArray[0]["minClose"] = 0;
		if($min!=0) $this->priceDtArray[0]["minClose"] = 100*($this->priceDtArray[0]["close"] - $min)/$min;
		//if($min!=0 && $max != 0) $this->priceDtArray[0]["minClose"] = 100*($max - $min)/$min;
		
		if($this->priceDtArray[0]["close"]!=0) $this->priceDtArray[0]["maxClose"] = 100*($max - $this->priceDtArray[0]["close"])/$this->priceDtArray[0]["close"];
		
		
		$length = count($this->priceDtArray);
		/*
		$count = 0;
		$megaCount = 0;
		$PERCENT = 4;
		for($i=0; $i < 40; $i++){
			if( substr($this->priceDtArray[$i]["stockDate"],0,10) == stock_analyser_ChartView::$startDate ) break;
			if($this->priceDtArray[$i+1]==null || $this->priceDtArray[$i+1]["close"]==0 || $this->priceDtArray[$i]["low"]==0) continue;
			$delta = 100*($this->priceDtArray[$i]["close"] - $this->priceDtArray[$i+1]["close"]) / $this->priceDtArray[$i+1]["close"];
			$deltaAbs = abs($delta);
			if($deltaAbs >= 10){
				$this->priceDtArray[0]["isDevided"] = 1;
				continue;
			}
			$megaCount += $deltaAbs;
			$count++;
		}
		
		if($count!=0) $megaCount = ($megaCount)/$count;
		$this->priceDtArray[0]["megaCount"] = $megaCount;
		*/
		
		
		$min = 999999;
		$imin = 0;
		$max = 0;
		$imax = 0;
		$min5 = 999999;
		$max5 = 0;
		$min20 = 999999;
		$max20 = 0;
		$greatAll = 0;
		for($i=0; $i < $length; $i++){
			//////////////if( substr($this->priceDtArray[$i]["stockDate"],0,10) == stock_analyser_ChartView::$startDate ) break;
			if($this->priceDtArray[$i]["close"] < $min){
				$min = $this->priceDtArray[$i]["close"];
				$imin = $i;
			}
			if($this->priceDtArray[$i]["close"] > $max){
				$max = $this->priceDtArray[$i]["close"];
				$imax = $i;
			}
			if($i<6){
				if($this->priceDtArray[$i]["close"] < $min5){
					$min5 = $this->priceDtArray[$i]["close"];
				}
				if($this->priceDtArray[$i]["close"] > $max5){
					$max5 = $this->priceDtArray[$i]["close"];
				}
			}
			if($i<21){
				if($this->priceDtArray[$i]["close"] < $min20){
					$min20 = $this->priceDtArray[$i]["close"];
				}
				if($this->priceDtArray[$i]["close"] > $max20){
					$max20 = $this->priceDtArray[$i]["close"];
				}
			}
			if($i>=1 && $i<=10){
				//if( $this->priceDtArray[0]["high"] >= $this->priceDtArray[$i]["close"]) $greatAll ++;
				if( 1.05*$this->priceDtArray[0]["close"] >= $this->priceDtArray[$i]["close"]) $greatAll ++;
			}
		}
	
		$this->priceDtArray[0]["greatAll"] = $greatAll;
		
		$this->priceDtArray[0]["iminZ"] = $imin;
		$this->priceDtArray[0]["minZ"] = $min;
		$this->priceDtArray[0]["imaxZ"] = $imax;
		$this->priceDtArray[0]["maxZ"] = $max;
		
		if($min!=0 && $max!=0) $this->priceDtArray[0]["minMaxZ"] = 100*($max - $min)/$min;
		if($min!=0) $this->priceDtArray[0]["minCloseZ"] = 100*($this->priceDtArray[0]["close"] - $min)/$min;
		
		if($min5!=0 && $max5!=0) $this->priceDtArray[0]["minMax5"] = 100*($max5 - $min5)/$min5;
		if($min5!=0) $this->priceDtArray[0]["minClose5"] = 100*(1.05*$this->priceDtArray[0]["close"] - $min5)/$min5;
		
		if($min20!=0 && $max20!=0) $this->priceDtArray[0]["minMax20"] = 100*($max20 - $min20)/$min20;
		if($min20!=0) $this->priceDtArray[0]["minClose20"] = 100*($this->priceDtArray[0]["close"] - $min20)/$min20;
		
		if($this->priceDtArray[0]["close"]!=0) $this->priceDtArray[0]["maxCloseZ"] = 100*($max - $this->priceDtArray[0]["close"])/$this->priceDtArray[0]["close"];
	}
}
